home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume15 / ggems / part01 next >
Encoding:
Text File  |  1990-10-14  |  59.6 KB  |  2,052 lines

  1. Newsgroups: comp.sources.misc
  2. X-UNIX-From: craig@weedeater.math.yale.edu
  3. subject: v15i023: Graphics Gems, Part 1/5
  4. from: Craig Kolb <craig@weedeater.math.yale.edu>
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 15, Issue 23
  8. Submitted-by: Craig Kolb <craig@weedeater.math.yale.edu>
  9. Archive-name: ggems/part01
  10.  
  11. [Going on my third week of this, I'm afraid it's gotten beyond the point where
  12. I can keep up.  I am therefore looking for a new moderator for this newsgroup.
  13. Send mail to comp.sources-misc-request@uunet.uu.net if you are interested.
  14. (Do NOT send mail to allbery@uunet.uu.net!  I haven't read read my mail there
  15. for at least two weeks.)  ++bsa]
  16.  
  17. #! /bin/sh
  18. # This is a shell archive.  Remove anything before this line, then unpack
  19. # it by saving it into a file and typing "sh file".  To overwrite existing
  20. # files, type "sh file -c".  You can also feed this as standard input via
  21. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  22. # will see the following message at the end:
  23. #        "End of archive 1 (of 5)."
  24. # Contents:  2DClip 2DClip/Makefile 2DClip/box.h 2DClip/line.h AALines
  25. #   AALines/00README AALines/AALines.h AALines/AAMain.c
  26. #   AALines/LongConst.h AALines/Makefile AALines/utah.h BinRec.c
  27. #   CircleRect.c DigitalLine.c FastJitter.c FixedTrig.c HSLtoRGB.c
  28. #   Hash3D.c HypotApprox.c MANIFEST MatrixOrtho.c PixelInteger.c
  29. #   PolyScan PolyScan/Makefile README RGBTo4Bits.c RayBox.c
  30. #   RayPolygon.c Sturm Sturm/Makefile Sturm/solve.h Sturm/util.c
  31. #   TransBox.c ViewTrans.c
  32. # Wrapped by craig@weedeater on Fri Oct 12 15:53:11 1990
  33. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  34. if test ! -d '2DClip' ; then
  35.     echo shar: Creating directory \"'2DClip'\"
  36.     mkdir '2DClip'
  37. fi
  38. if test -f '2DClip/Makefile' -a "${1}" != "-c" ; then 
  39.   echo shar: Will not clobber existing file \"'2DClip/Makefile'\"
  40. else
  41. echo shar: Extracting \"'2DClip/Makefile'\" \(212 characters\)
  42. sed "s/^X//" >'2DClip/Makefile' <<'END_OF_FILE'
  43. XLIBFILE = ../gemslib.a
  44. X
  45. XCFLAGS = $(GENCFLAGS) -I..
  46. X
  47. XOFILES = clip.o bio.o cross.o
  48. X
  49. X$(LIBFILE): $(OFILES)
  50. X    ar rcs $(LIBFILE) $(OFILES)
  51. X
  52. Xclean:
  53. X    /bin/rm -f clip.o bio.o cross.o
  54. X
  55. X$(OFILES): line.h ../GraphicsGems.h
  56. END_OF_FILE
  57. if test 212 -ne `wc -c <'2DClip/Makefile'`; then
  58.     echo shar: \"'2DClip/Makefile'\" unpacked with wrong size!
  59. fi
  60. # end of '2DClip/Makefile'
  61. fi
  62. if test -f '2DClip/box.h' -a "${1}" != "-c" ; then 
  63.   echo shar: Will not clobber existing file \"'2DClip/box.h'\"
  64. else
  65. echo shar: Extracting \"'2DClip/box.h'\" \(186 characters\)
  66. sed "s/^X//" >'2DClip/box.h' <<'END_OF_FILE'
  67. X
  68. X/* 
  69. X * file box.h
  70. X *    a short include file is better then no include file
  71. X */
  72. Xtypedef    struct    {        /* guess what this is        */
  73. X    long    _lowx;
  74. X    long    _lowy;
  75. X    long    _highx;
  76. X    long    _highy;
  77. X} BOX;
  78. X
  79. END_OF_FILE
  80. if test 186 -ne `wc -c <'2DClip/box.h'`; then
  81.     echo shar: \"'2DClip/box.h'\" unpacked with wrong size!
  82. fi
  83. # end of '2DClip/box.h'
  84. fi
  85. if test -f '2DClip/line.h' -a "${1}" != "-c" ; then 
  86.   echo shar: Will not clobber existing file \"'2DClip/line.h'\"
  87. else
  88. echo shar: Extracting \"'2DClip/line.h'\" \(1795 characters\)
  89. sed "s/^X//" >'2DClip/line.h' <<'END_OF_FILE'
  90. X
  91. X/*
  92. XTwo-Dimensional Clipping: A Vector Based Approach
  93. Xby Hans Spoelder and Fons Ullings
  94. Xfrom "Graphics Gems", Academic Press, 1990
  95. X*/
  96. X
  97. X
  98. X/*
  99. X * file line.h
  100. X *     contains major definitions for the clipping routines
  101. X *
  102. X */
  103. X#define    NFAC        10        /* discrete measure    */
  104. X    
  105. X#define    SCALE        (1 << NFAC)    /* 1024 points/cm    */
  106. X#define    TO_INT(X)    ((int)((X)*SCALE))
  107. X#define    TO_FLT(X)    (((float)(X))/SCALE)
  108. X
  109. X#define    COINCIDE        1        /* what do the lines do    */
  110. X#define    PARALLEL        2
  111. X#define    CROSS        3
  112. X#define    NO_CROSS        4
  113. X
  114. X#define    STD            0        /* crossing types    */
  115. X#define    DELAY        1
  116. X
  117. X#define    CLIP_NORMAL    1
  118. X
  119. Xtypedef    struct    {        /* holds a point    */
  120. X    long    _x;            /* holds x coordinate    */
  121. X    long    _y;            /* holds y coordinate    */
  122. X} POINT;
  123. X
  124. Xtypedef    struct    {        /* holds a cross point    */
  125. X    POINT    _p;            /* holds the solution    */
  126. X    short    _type;        /* more information    */
  127. X} CLIST;
  128. X    
  129. Xstruct    segment    {            /* holds a segment    */
  130. X    POINT    _from;            /* start coordinates    */
  131. X    POINT    _to;            /* stop coordinates    */
  132. X    struct    segment    *_next;
  133. X    struct    segment    *_prev;
  134. X};
  135. X
  136. X
  137. X#define    SEGMENT        struct segment
  138. X
  139. Xstruct    contour {            /* holds a contour    */
  140. X    short    _no;            /* contour counter    */
  141. X    short    _status;            /* holds information    */
  142. X    short    _cnt;            /* number of elements    */
  143. X    SEGMENT    *_s;            /* the segments        */
  144. X    struct    contour *_next;    /* linked list        */
  145. X    long    _minx;            /* coordinates of box    */
  146. X    long    _miny;
  147. X    long    _maxx;
  148. X    long    _maxy;
  149. X};
  150. X
  151. X#define    CONTOUR        struct contour
  152. X
  153. X#define    ACTIVE        01        /* polygon attributes    */
  154. X#define    NORMAL        02
  155. X
  156. X#define    SET_ON(p)    ((p)->_status |=  ACTIVE)
  157. X#define    SET_NORMAL(p)    ((p)->_status |= NORMAL)
  158. X
  159. X#define    SET_OFF(p)    ((p)->_status &= ~ACTIVE)
  160. X#define    SET_INVERSE(p)    ((p)->_status &= ~NORMAL)
  161. X
  162. X#define    IS_ON(p)    ((p)->_status & ACTIVE)
  163. X#define    IS_NORMAL(p)    ((p)->_status & NORMAL)
  164. X
  165. Xextern    CONTOUR    *CL;
  166. X
  167. XCONTOUR    *get_contour_ptr();
  168. X
  169. Xextern    short    C_COUNT;
  170. END_OF_FILE
  171. if test 1795 -ne `wc -c <'2DClip/line.h'`; then
  172.     echo shar: \"'2DClip/line.h'\" unpacked with wrong size!
  173. fi
  174. # end of '2DClip/line.h'
  175. fi
  176. if test ! -d 'AALines' ; then
  177.     echo shar: Creating directory \"'AALines'\"
  178.     mkdir 'AALines'
  179. fi
  180. if test -f 'AALines/00README' -a "${1}" != "-c" ; then 
  181.   echo shar: Will not clobber existing file \"'AALines/00README'\"
  182. else
  183. echo shar: Extracting \"'AALines/00README'\" \(1074 characters\)
  184. sed "s/^X//" >'AALines/00README' <<'END_OF_FILE'
  185. XThis group of files is a simple demonstration of an anti-aliased line
  186. Xrenderer from _Grahpics_Gems_.  Files in the release are:
  187. X
  188. X  00README -- This information file.
  189. X
  190. X  Makefile -- Makefile for creating the demo executable.
  191. X
  192. X  AALines.h -- Include file for demo source files.
  193. X
  194. X  AALines.c -- Rendering code from _Grahpics_Gems_ pages 690-693.
  195. X
  196. X  AATables.c -- Initialization code for frame buffer and lookup tables.
  197. X
  198. X  AAMain.c -- Calling routine for the renderer.
  199. X
  200. X  utah.h -- Include file for friendly Utah RLE front end.
  201. X
  202. X  utah.c -- Source for friendly Utah RLE front end.
  203. X
  204. XAs it is written, the program dumps its frame buffer to a Utah RLE
  205. Xfile.  You need to obtain the Utah RLE library from another source;
  206. Xtry the following FTP sites:
  207. X
  208. X        cs.utah.edu (128.110.4.21)
  209. X        weedeater.math.yale.edu (130.132.23.17)
  210. X        freebie.engin.umich.edu (35.2.68.23)
  211. X
  212. XIt should be fairly easy to dump the frame buffer to another type
  213. Xof file, or straight to a display device.  See AAMain.c.
  214. X
  215. XHave fun.
  216. X
  217. X    -- Kelvin Thompson, 18 August 1990
  218. X       kelvin@cs.utexas.edu
  219. END_OF_FILE
  220. if test 1074 -ne `wc -c <'AALines/00README'`; then
  221.     echo shar: \"'AALines/00README'\" unpacked with wrong size!
  222. fi
  223. # end of 'AALines/00README'
  224. fi
  225. if test -f 'AALines/AALines.h' -a "${1}" != "-c" ; then 
  226.   echo shar: Will not clobber existing file \"'AALines/AALines.h'\"
  227. else
  228. echo shar: Extracting \"'AALines/AALines.h'\" \(1371 characters\)
  229. sed "s/^X//" >'AALines/AALines.h' <<'END_OF_FILE'
  230. X/*  FILENAME:  AALines.h  [revised 17 AUG 90]
  231. X
  232. X    AUTHOR:  Kelvin Thompson
  233. X
  234. X    DESCRIPTION:  Symbols and globals for the anti-aliased line
  235. X      renderer.
  236. X
  237. X    #INCLUDED IN:
  238. X      AAMain.c -- Calling routine for renderer.
  239. X      AATables.c -- Initialization routines for lookup tables.
  240. X      AALines.c -- Rendering code.
  241. X*/
  242. X
  243. X/* frame buffer to hold the anti-aliased line */
  244. X#define xpix 60
  245. X#define ypix 60
  246. Xextern char *fbuff;
  247. X
  248. X/* macros to access the frame buffer */
  249. X#define PIXADDR(xx,yy) (fbuff+(yy)*xpix+(xx))
  250. X#define PIXINC(dx,dy)  ((dy)*xpix+(dx))
  251. X
  252. X/* fixed-point data types and macros */
  253. Xtypedef int FX;
  254. Xtypedef unsigned int UFX;
  255. X#define FX_FRACBITS 16  /* bits of fraction in FX format */
  256. X#define FX_0        0   /* zero in fixed-point format */
  257. X#define FLOAT_TO_FX(flt)  ((FX)((flt)*(1<<FX_FRACBITS)+0.5))
  258. X
  259. X/* some important constants */
  260. X#define PI      3.1415926535897932384626433832795028841971693993751
  261. X#define SQRT_2  1.4142135623730950488016887242096980785696718753769
  262. X
  263. X/* square-root function globals */
  264. Xextern UFX *sqrtfunc;
  265. Xextern int sqrtcells;
  266. Xextern int sqrtshift;
  267. X#define SQRTFUNC(fxval)  (sqrtfunc[ (fxval) >> sqrtshift ])
  268. X
  269. X/* AA globals */
  270. Xextern float line_r;  /* line radius */
  271. Xextern float pix_r;   /* pixel radius */
  272. Xextern FX *coverage;
  273. Xextern int covercells;
  274. Xextern int covershift;
  275. X#define COVERAGE(fxval) (coverage[ (fxval) >> covershift ])
  276. END_OF_FILE
  277. if test 1371 -ne `wc -c <'AALines/AALines.h'`; then
  278.     echo shar: \"'AALines/AALines.h'\" unpacked with wrong size!
  279. fi
  280. # end of 'AALines/AALines.h'
  281. fi
  282. if test -f 'AALines/AAMain.c' -a "${1}" != "-c" ; then 
  283.   echo shar: Will not clobber existing file \"'AALines/AAMain.c'\"
  284. else
  285. echo shar: Extracting \"'AALines/AAMain.c'\" \(1578 characters\)
  286. sed "s/^X//" >'AALines/AAMain.c' <<'END_OF_FILE'
  287. X/*  FILENAME:  AAMain.c  [revised 17 AUG 90]
  288. X
  289. X    AUTHOR:  Kelvin Thompson
  290. X
  291. X    DESCRIPTION:  Calling routine for anti-aliased line renderer.
  292. X      This routine calls the line renderer to draw a single
  293. X      anti-aliased line into a small frame buffer.  The
  294. X      routine then dumps the frame buffer to a Utah RLE file
  295. X      'anti.rle'.
  296. X
  297. X    LINK WITH:
  298. X      utah.h -- Definitions for friendly Utah RLE front end.
  299. X      AALines.h -- Shared tables, symbols, etc. for renderer.
  300. X      AALines.c -- Rendering code.
  301. X      AATables.c -- Table initialization.
  302. X*/
  303. X
  304. X#include <stdio.h>
  305. X#include <math.h>
  306. X#include "AALines.h"
  307. X#include "utah.h"
  308. X
  309. X
  310. X
  311. Xmain ( argc, argv )
  312. Xint argc;
  313. Xchar *argv[];
  314. X{
  315. Xint i;
  316. Xchar *scanptr;
  317. Xint x1,y1,x2,y2;
  318. X
  319. X/* initialize frame buffer and look-up tables */
  320. XAnti_Init();
  321. X
  322. X/* set line endpoints */
  323. Xx1 =  2;  y1 =  2;
  324. Xx2 = 25;  y2 = 55;
  325. X
  326. X/* render anti-aliased line to a frame buffer */
  327. XAnti_Line( x1,y1, x2,y2 );
  328. X
  329. X
  330. X/* The code below dumps the frame buffer to a Utah RLE file.
  331. X** It should be pretty easy to rewrite so that it dumps to
  332. X** any other kind of output file...or straight to a display
  333. X** device.  The frame buffer is an array of characters
  334. X** starting at 'fbuff' with size 'xpix' by 'ypix'.  */
  335. X
  336. X  {
  337. X  /* thanks to A.T. Campbell for the friendly front end */
  338. X  UTAH_FILE *picout;
  339. X  picout = utah_write_init( "anti.rle", xpix, ypix );
  340. X  if ( picout == NULL )
  341. X    { perror("anti.rle");  exit(1); }
  342. X  for ( i=0; i<ypix; i++ )
  343. X    {
  344. X    scanptr = &fbuff[i*xpix];
  345. X    utah_write_rgb( picout, scanptr, scanptr, scanptr );
  346. X    }
  347. X  utah_write_close(picout);
  348. X  }
  349. X}
  350. END_OF_FILE
  351. if test 1578 -ne `wc -c <'AALines/AAMain.c'`; then
  352.     echo shar: \"'AALines/AAMain.c'\" unpacked with wrong size!
  353. fi
  354. # end of 'AALines/AAMain.c'
  355. fi
  356. if test -f 'AALines/LongConst.h' -a "${1}" != "-c" ; then 
  357.   echo shar: Will not clobber existing file \"'AALines/LongConst.h'\"
  358. else
  359. echo shar: Extracting \"'AALines/LongConst.h'\" \(1701 characters\)
  360. sed "s/^X//" >'AALines/LongConst.h' <<'END_OF_FILE'
  361. X/*  FILENAME:   LongConst.h  [revised 18 AUG 90]
  362. X
  363. X    AUTHOR:  Kelvin Thompson
  364. X
  365. X    DESCRIPTION:  High-precision constants.  If this file is included
  366. X      in the same file as GraphicsGems.h, this file must come *after*
  367. X      GraphicsGems.h.  (It's okay to use this file without GraphicsGems.h.)
  368. X
  369. X        The standard _Graphics_Gems_ include file has some constants
  370. X      that do not have full double-precision accuracy.  This file
  371. X      has the constants to a ridiculously high precision.  See pages
  372. X      434-435 of _Graphics_Gems_.  I got the constants from Mathematica.
  373. X      
  374. X        Also, this file has a constant and macro for finding the base-two
  375. X      logarithm of a number.
  376. X*/
  377. X
  378. X/* prevent multiple inclusion */
  379. X#ifndef __LONGCONST_H__
  380. X#define __LONGCONST_H__
  381. X
  382. X/* first get rid of stuff from GraphicsGems.h */
  383. X#undef PI
  384. X#undef PITIMES2
  385. X#undef PIOVER2
  386. X#undef E
  387. X#undef SQRT2
  388. X#undef SQRT3
  389. X#undef GOLDEN
  390. X#undef DTOR
  391. X#undef RTOD
  392. X
  393. X/* re-define basic constants with high precision */
  394. X#define PI     3.141592653589793238462643383279502884197169399375105820975
  395. X#define E      2.718281828459045235360287471352662497757247093699959574967
  396. X#define SQRT2  1.414213562373095048801688724209698078569671875376948073177
  397. X#define SQRT3  1.732050807568877293527446341505872366942805253810380628056
  398. X#define GOLDEN 1.618033988749894848204586834365638117720309179805762862135
  399. X
  400. X/* re-define derived constants */
  401. X#define PITIMES2  (2.0*PI)
  402. X#define PIOVER2   (0.5*PI)
  403. X#define DTOR      (PI/180.0)
  404. X#define RTOD      (180.0/PI)
  405. X
  406. X/* macro and constant for base 2 logarithm */
  407. X#define LN2    0.693147180559945309417232121458176568075500134360255254121
  408. X#define LOG2(val) (log(val)*(1.0/LN_2))
  409. X
  410. X#endif  /* __LONGCONST_H__ */
  411. END_OF_FILE
  412. if test 1701 -ne `wc -c <'AALines/LongConst.h'`; then
  413.     echo shar: \"'AALines/LongConst.h'\" unpacked with wrong size!
  414. fi
  415. # end of 'AALines/LongConst.h'
  416. fi
  417. if test -f 'AALines/Makefile' -a "${1}" != "-c" ; then 
  418.   echo shar: Will not clobber existing file \"'AALines/Makefile'\"
  419. else
  420. echo shar: Extracting \"'AALines/Makefile'\" \(462 characters\)
  421. sed "s/^X//" >'AALines/Makefile' <<'END_OF_FILE'
  422. X# FILENAME:  Makefile  [revised 18 AUG 90]
  423. X# 
  424. X# AUTHOR:  Kelvin Thompson
  425. X# 
  426. X# DESCRIPTION:  Makefile for anti-aliased line rendering demo.
  427. X
  428. X# locations of Utah RLE information
  429. XUTAH_RLE_INCLUDE_DIR = /public/graphics/rle/include
  430. XUTAH_RLE_LIB_FILE = /p/lib/librle.a
  431. X
  432. XCFLAGS = -I$(UTAH_RLE_INCLUDE_DIR)
  433. X
  434. XOBJS = AAMain.o AALines.o AATables.o utah.o
  435. X
  436. X%.o : %.c
  437. X    $(CC) -c $(CFLAGS) $(CPPFLAGS) $<
  438. X
  439. XAALine : $(OBJS)
  440. X    cc $(CFLAGS) -o $@ $(OBJS) $(UTAH_RLE_LIB_FILE) -lm
  441. END_OF_FILE
  442. if test 462 -ne `wc -c <'AALines/Makefile'`; then
  443.     echo shar: \"'AALines/Makefile'\" unpacked with wrong size!
  444. fi
  445. # end of 'AALines/Makefile'
  446. fi
  447. if test -f 'AALines/utah.h' -a "${1}" != "-c" ; then 
  448.   echo shar: Will not clobber existing file \"'AALines/utah.h'\"
  449. else
  450. echo shar: Extracting \"'AALines/utah.h'\" \(877 characters\)
  451. sed "s/^X//" >'AALines/utah.h' <<'END_OF_FILE'
  452. X/*
  453. X    file:        utah.h
  454. X    description:    interface to Utah RLE toolkit
  455. X    author:        A. T. Campbell
  456. X    date:        October 30, 1989
  457. X*/
  458. X
  459. X#ifndef UTAH_H
  460. X#define UTAH_H
  461. X
  462. X/******************************************************************************/
  463. X
  464. X/* include files */
  465. X#include "svfb_global.h"
  466. X
  467. X/******************************************************************************/
  468. X
  469. X/* type definitions */
  470. Xtypedef struct sv_globals UTAH_FILE;
  471. X
  472. X/******************************************************************************/
  473. X
  474. X/* return values */
  475. Xextern int        utah_read_close();
  476. Xextern UTAH_FILE    *utah_read_init();
  477. Xextern int        utah_read_pixels();
  478. Xextern int        utah_read_rgb();
  479. Xextern int        utah_write_close();
  480. Xextern UTAH_FILE    *utah_write_init();
  481. Xextern int        utah_write_pixels();
  482. Xextern int        utah_write_rgb();
  483. X
  484. X/******************************************************************************/
  485. X
  486. X#endif UTAH_H
  487. END_OF_FILE
  488. if test 877 -ne `wc -c <'AALines/utah.h'`; then
  489.     echo shar: \"'AALines/utah.h'\" unpacked with wrong size!
  490. fi
  491. # end of 'AALines/utah.h'
  492. fi
  493. if test -f 'BinRec.c' -a "${1}" != "-c" ; then 
  494.   echo shar: Will not clobber existing file \"'BinRec.c'\"
  495. else
  496. echo shar: Extracting \"'BinRec.c'\" \(1171 characters\)
  497. sed "s/^X//" >'BinRec.c' <<'END_OF_FILE'
  498. X/*
  499. X * Recording Animation in Binary Order for Progressive Temporal Refinement
  500. X * by Paul Heckbert
  501. X * from "Graphics Gems", Academic Press, 1990
  502. X */
  503. X
  504. X/*
  505. X * binrec.c: demonstrate binary recording order
  506. X *
  507. X * Paul Heckbert    Jan 90
  508. X */
  509. X
  510. X#include <stdio.h>
  511. X
  512. Xmain(ac, av)
  513. Xint ac;
  514. Xchar **av;
  515. X{
  516. X    int nframes, i, start_frame, repeat_count;
  517. X    if (ac!=2) {
  518. X    fprintf(stderr, "Usage: binrec <nframes>\n");
  519. X    exit(1);
  520. X    }
  521. X    nframes = atoi(av[1]);
  522. X
  523. X    printf("step startframe repeatcount\n");
  524. X    for (i=0; i<nframes; i++) {
  525. X    inside_out(nframes, i, &start_frame, &repeat_count);
  526. X    printf(" %2d     %2d          %2d\n", i, start_frame, repeat_count);
  527. X    }
  528. X}
  529. X
  530. X/*
  531. X * inside_out: turn a number "inside-out": a generalization of bit-reversal.
  532. X * For n = power of two, this is equivalent to bit-reversal.
  533. X *
  534. X * Turn the number a inside-out, yielding b.  If 0<=a<n then 0<=b<n.
  535. X * Also return r = min(n-b, largest power of 2 dividing b)
  536. X */
  537. X
  538. Xinside_out(n, a, b, r)
  539. Xint n, a, *b, *r;
  540. X{
  541. X    int k, m;
  542. X
  543. X    *r = m = n;
  544. X    for (*b=0, k=1; k<n; k<<=1)
  545. X    if (a<<1>=m) {
  546. X        if (*b==0) *r = k;
  547. X        *b += k;
  548. X        a -= m+1>>1;
  549. X        m >>= 1;
  550. X    }
  551. X    else m = m+1>>1;
  552. X    if (*r>n-*b) *r = n-*b;
  553. X}
  554. END_OF_FILE
  555. if test 1171 -ne `wc -c <'BinRec.c'`; then
  556.     echo shar: \"'BinRec.c'\" unpacked with wrong size!
  557. fi
  558. # end of 'BinRec.c'
  559. fi
  560. if test -f 'CircleRect.c' -a "${1}" != "-c" ; then 
  561.   echo shar: Will not clobber existing file \"'CircleRect.c'\"
  562. else
  563. echo shar: Extracting \"'CircleRect.c'\" \(1513 characters\)
  564. sed "s/^X//" >'CircleRect.c' <<'END_OF_FILE'
  565. X/* 
  566. XFast Circle-Rectangle Intersection Checking
  567. Xby Clifford A. Shaffer
  568. Xfrom "Graphics Gems", Academic Press, 1990
  569. X*/
  570. X
  571. X#include "GraphicsGems.h"
  572. X
  573. Xboolean Check_Intersect(R, C, Rad)
  574. X
  575. X/* Return TRUE iff rectangle R intersects circle with centerpoint C and
  576. X   radius Rad. */
  577. X Box2 *R;
  578. X Point2 *C;
  579. X double Rad;
  580. X{
  581. X double Rad2;
  582. X
  583. X Rad2 = Rad * Rad;
  584. X /* Translate coordinates, placing C at the origin. */
  585. X R->max.x -= C->x;  R->max.y -= C->y;
  586. X R->min.x -= C->x;  R->min.y -= C->y;
  587. X
  588. X if (R->max.x < 0)             /* R to left of circle center */
  589. X       if (R->max.y < 0)         /* R in lower left corner */
  590. X             return ((R->max.x * R->max.x + R->max.y * R->max.y) < Rad2);
  591. X       else if (R->min.y > 0)     /* R in upper left corner */
  592. X             return ((R->max.x * R->max.x + R->min.y * R->min.y) < Rad2);
  593. X       else                     /* R due West of circle */
  594. X             return(ABS(R->max.x) < Rad);
  595. X     else if (R->min.x > 0)      /* R to right of circle center */
  596. X           if (R->max.y < 0)     /* R in lower right corner */
  597. X                 return ((R->min.x * R->min.x) < Rad2);
  598. X       else if (R->min.y > 0)      /* R in upper right corner */
  599. X             return ((R->min.x * R->min.x + R->min.y + R->min.y) < Rad2);
  600. X       else                 /* R due East of circle */
  601. X             return (R->min.x < Rad);
  602. X     else                /* R on circle vertical centerline */
  603. X           if (R->max.y < 0)     /* R due South of circle */
  604. X             return (ABS(R->max.y) < Rad);
  605. X       else if (R->min.y > 0)      /* R due North of circle */
  606. X             return (R->min.y < Rad);
  607. X       else                 /* R contains circle centerpoint */
  608. X             return(TRUE);
  609. X}     
  610. END_OF_FILE
  611. if test 1513 -ne `wc -c <'CircleRect.c'`; then
  612.     echo shar: \"'CircleRect.c'\" unpacked with wrong size!
  613. fi
  614. # end of 'CircleRect.c'
  615. fi
  616. if test -f 'DigitalLine.c' -a "${1}" != "-c" ; then 
  617.   echo shar: Will not clobber existing file \"'DigitalLine.c'\"
  618. else
  619. echo shar: Extracting \"'DigitalLine.c'\" \(943 characters\)
  620. sed "s/^X//" >'DigitalLine.c' <<'END_OF_FILE'
  621. X/*
  622. X * Digital Line Drawing
  623. X * by Paul Heckbert
  624. X * from "Graphics Gems", Academic Press, 1990
  625. X */
  626. X
  627. X/*
  628. X * digline: draw digital line from (x1,y1) to (x2,y2),
  629. X * calling a user-supplied procedure at each pixel.
  630. X * Does no clipping.  Uses Bresenham's algorithm.
  631. X *
  632. X * Paul Heckbert    3 Sep 85
  633. X */
  634. X
  635. X#include "GraphicsGems.h"
  636. X
  637. Xdigline(x1, y1, x2, y2, dotproc)
  638. Xint x1, y1, x2, y2;
  639. Xvoid (*dotproc)();
  640. X{
  641. X    int d, x, y, ax, ay, sx, sy, dx, dy;
  642. X
  643. X    dx = x2-x1;  ax = ABS(dx)<<1;  sx = SGN(dx);
  644. X    dy = y2-y1;  ay = ABS(dy)<<1;  sy = SGN(dy);
  645. X
  646. X    x = x1;
  647. X    y = y1;
  648. X    if (ax>ay) {        /* x dominant */
  649. X    d = ay-(ax>>1);
  650. X    for (;;) {
  651. X        (*dotproc)(x, y);
  652. X        if (x==x2) return;
  653. X        if (d>=0) {
  654. X        y += sy;
  655. X        d -= ax;
  656. X        }
  657. X        x += sx;
  658. X        d += ay;
  659. X    }
  660. X    }
  661. X    else {            /* y dominant */
  662. X    d = ax-(ay>>1);
  663. X    for (;;) {
  664. X        (*dotproc)(x, y);
  665. X        if (y==y2) return;
  666. X        if (d>=0) {
  667. X        x += sx;
  668. X        d -= ay;
  669. X        }
  670. X        y += sy;
  671. X        d += ax;
  672. X    }
  673. X    }
  674. X}
  675. END_OF_FILE
  676. if test 943 -ne `wc -c <'DigitalLine.c'`; then
  677.     echo shar: \"'DigitalLine.c'\" unpacked with wrong size!
  678. fi
  679. # end of 'DigitalLine.c'
  680. fi
  681. if test -f 'FastJitter.c' -a "${1}" != "-c" ; then 
  682.   echo shar: Will not clobber existing file \"'FastJitter.c'\"
  683. else
  684. echo shar: Extracting \"'FastJitter.c'\" \(1720 characters\)
  685. sed "s/^X//" >'FastJitter.c' <<'END_OF_FILE'
  686. X/*
  687. X * Efficient Generation of Sampling Jitter Using Look-up Tables
  688. X * by Joseph M. Cychosz
  689. X * from "Graphics Gems", Academic Press, 1990
  690. X */
  691. X
  692. X/*      Jitter.c - Sampling jitter generation routines.          
  693. X/*
  694. X/*    Description:
  695. X/*        Jitter.c contains the routines for generation of sampling  
  696. X/*        jitter using look-up tables.
  697. X/*
  698. X/*    Contents:
  699. X/*        Jitter1    Generate random jitter function 1.
  700. X/*        Jitter2    Generate random jitter function 2.            
  701. X/*        JitterInit    Initialize look-up tables.
  702. X/* */
  703. X
  704. X#define            NRAN    1024    /* Random number table length    */
  705. X
  706. Xstatic    double    URANX[NRAN],        /* Random number tables        */
  707. X                URANY[NRAN];
  708. Xstatic    int        IRAND[NRAN];    /* Shuffle table    */
  709. Xstatic    int        MASK = NRAN-1;    /* Mask for jitter mod function */
  710. Xextern    double    xranf();        /* Random number generator pro- */
  711. X                    /* ducing uniform numbers 0 to 1 */
  712. X
  713. X/*      Jitter1 - Generate random jitter.     */
  714. X
  715. Xvoid    Jitter1    (x,y,s,xj,yj)
  716. X        int    x, y;        /* Pixel location     */
  717. X        int    s;        /* Sample number for the pixel */
  718. X        double    *xj, *yj;    /* Jitter (x,y)    */
  719. X{
  720. X        *xj = URANX[ (x + (y<<2) + IRAND[(x+s)&MASK]) & MASK ];
  721. X        *yj = URANY[ (y + (x<<2) + IRAND[(y+s)&MASK]) & MASK ];
  722. X}
  723. X
  724. X
  725. X
  726. X/*      Jitter2 - Generate random jitter.      */
  727. X
  728. Xvoid    Jitter2    (x,y,s,xj,yj)
  729. X        int    x, y;        /* Pixel location  */
  730. X        int    s;        /* Sample number for the pixel */
  731. X        double    *xj, *yj;    /* Jitter (x,y)    */
  732. X{
  733. X        *xj = URANX[ ((x | (y<<2)) + IRAND[(x+s)&MASK]) & MASK ];
  734. X        *yj = URANY[ ((y | (x<<2)) + IRAND[(y+s)&MASK]) & MASK ];
  735. X}
  736. X
  737. X
  738. X/*      JitterInit - Initialize look-up tables.      */
  739. X
  740. Xvoid    JitterInit    ()
  741. X{
  742. X        int    i;
  743. X
  744. X        for ( i = 0 ; i < NRAN ; i++ ) URANX[i] = xranf();
  745. X        for ( i = 0 ; i < NRAN ; i++ ) URANY[i] = xranf();
  746. X        for ( i = 0 ; i < NRAN ; i++ ) IRAND[i] = (int) (NRAN *
  747. X                        xranf());
  748. X}
  749. END_OF_FILE
  750. if test 1720 -ne `wc -c <'FastJitter.c'`; then
  751.     echo shar: \"'FastJitter.c'\" unpacked with wrong size!
  752. fi
  753. # end of 'FastJitter.c'
  754. fi
  755. if test -f 'FixedTrig.c' -a "${1}" != "-c" ; then 
  756.   echo shar: Will not clobber existing file \"'FixedTrig.c'\"
  757. else
  758. echo shar: Extracting \"'FixedTrig.c'\" \(1725 characters\)
  759. sed "s/^X//" >'FixedTrig.c' <<'END_OF_FILE'
  760. X/* 
  761. XFixed-Point Trigonometry with CORDIC Iterations
  762. Xby Ken Turkowski
  763. Xfrom "Graphics Gems", Academic Press, 1990
  764. X*/
  765. X
  766. X#define COSCALE 0x22c2dd1c /* 0.271572 */
  767. X#define QUARTER ((int)(3.141592654 / 2.0 * (1 << 28)))
  768. Xstatic long arctantab[32] = {  /* MS 4 integral bits for radians */
  769. X    297197971, 210828714, 124459457, 65760959, 33381290, 16755422,
  770. X    8385879, 4193963, 2097109, 1048571, 524287, 262144, 131072,
  771. X    65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64,
  772. X    32, 16, 8, 4, 2, 1, 0, 0,
  773. X};
  774. X
  775. XCordicRotate(px, py, theta)
  776. Xlong *px, *py;
  777. Xregister long theta;    /* Assume that abs(theta) <= pi */
  778. X{
  779. X    register int i;
  780. X    register long x = *px, y = *py, xtemp;
  781. X    register long *arctanptr = arctantab;
  782. X
  783. X    /* The -1 may need to be pulled out and done as a left shift */
  784. X    for (i = -1; i <= 28; i++) {
  785. X        if (theta < 0) {
  786. X            xtemp = x + (y >> i);
  787. X            y     = y - (x >> i);
  788. X            x = xtemp;
  789. X            theta += *arctanptr++;
  790. X        } else {
  791. X            xtemp = x - (y >> i);    
  792. X            y     = y + (x >> i);
  793. X            x = xtemp;    
  794. X            theta -= *arctanptr++;
  795. X        }
  796. X    }
  797. X
  798. X    *px = frmul(x, COSCALE); /* Compensate for CORDIC enlargement */
  799. X    *py = frmul(y, COSCALE); /* frmul(a,b)=(a*b)>>31, high part  */
  800. X                 /* of 64-bit product */
  801. X}
  802. X
  803. X
  804. X
  805. X
  806. XCordicPolarize(argx, argy)
  807. Xlong *argx, *argy;    /* We assume these are already in the */
  808. X                    /*  right half plane */
  809. X{
  810. X    register long theta, yi, i;
  811. X    register long x = *argx, y = *argy;
  812. X    register long *arctanptr = arctantab;
  813. X    for (i = -1; i <= 28; i++) {
  814. X        if (y < 0) {        /* Rotate positive */
  815. X            yi = y + (x >> i);
  816. X            x  = x - (y >> i);
  817. X            y  = yi;
  818. X            theta -= *arctanptr++;
  819. X        } else {         /* Rotate negative */
  820. X            yi = y - (x >> i);
  821. X            x  = x + (y >> i);
  822. X            y  = yi;
  823. X            theta += *arctanptr++;
  824. X        }
  825. X    }
  826. X
  827. X    *argx = frmul(x, COSCALE);
  828. X    *argy = theta;
  829. X}
  830. END_OF_FILE
  831. if test 1725 -ne `wc -c <'FixedTrig.c'`; then
  832.     echo shar: \"'FixedTrig.c'\" unpacked with wrong size!
  833. fi
  834. # end of 'FixedTrig.c'
  835. fi
  836. if test -f 'HSLtoRGB.c' -a "${1}" != "-c" ; then 
  837.   echo shar: Will not clobber existing file \"'HSLtoRGB.c'\"
  838. else
  839. echo shar: Extracting \"'HSLtoRGB.c'\" \(1736 characters\)
  840. sed "s/^X//" >'HSLtoRGB.c' <<'END_OF_FILE'
  841. X/*
  842. XA Fast HSL-to-RGB Transform
  843. Xby Ken Fishkin
  844. Xfrom "Graphics Gems", Academic Press, 1990
  845. X*/
  846. X
  847. X#include <math.h>
  848. X#include <stdio.h>
  849. X#include "GraphicsGems.h"
  850. X
  851. X    /*
  852. X     * RGB-HSL transforms.
  853. X     * Ken Fishkin, Pixar Inc., January 1989.
  854. X     */
  855. X
  856. X    /*
  857. X    * given r,g,b on [0 ... 1],
  858. X    * return (h,s,l) on [0 ... 1]
  859. X    */
  860. Xvoid
  861. XRGB_to_HSL    (r,g,b,h,s,l)
  862. Xdouble     r,g,b;
  863. Xdouble *h, *s, *l;
  864. X{
  865. X    double v;
  866. X    double m;
  867. X    double vm;
  868. X    double r2, g2, b2;
  869. X
  870. X    v = MAX(r,g);
  871. X    v = MAX(v,b);
  872. X    m = MIN(r,g);
  873. X    m = MIN(m,b);
  874. X
  875. X    if ((*l = (m + v) / 2.0) <= 0.0) return;
  876. X    if ((*s = vm = v - m) > 0.0) {
  877. X        *s /= (*l <= 0.5) ? (v + m ) :
  878. X            (2.0 - v - m) ;
  879. X    } else
  880. X    return;
  881. X
  882. X
  883. X    r2 = (v - r) / vm;
  884. X    g2 = (v - g) / vm;
  885. X    b2 = (v - b) / vm;
  886. X
  887. X    if (r == v)
  888. X        *h = (g == m ? 5.0 + b2 : 1.0 - g2);
  889. X    else if (g == v)
  890. X        *h = (b == m ? 1.0 + r2 : 3.0 - b2);
  891. X    else
  892. X        *h = (r == m ? 3.0 + g2 : 5.0 - r2);
  893. X
  894. X        *h /= 6;
  895. X    }
  896. X
  897. X    /*
  898. X     * given h,s,l on [0..1],
  899. X     * return r,g,b on [0..1]
  900. X     */
  901. Xvoid
  902. XHSL_to_RGB(h,sl,l,r,g,b)
  903. Xdouble     h,sl,l;
  904. Xdouble     *r, *g, *b;
  905. X{
  906. X    double v;
  907. X
  908. X    v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
  909. X    if (v <= 0) {
  910. X        *r = *g = *b = 0.0;
  911. X    } else {
  912. X        double m;
  913. X        double sv;
  914. X        int sextant;
  915. X        double fract, vsf, mid1, mid2;
  916. X
  917. X        m = l + l - v;
  918. X        sv = (v - m ) / v;
  919. X        h *= 6.0;
  920. X        sextant = h;    
  921. X        fract = h - sextant;
  922. X        vsf = v * sv * fract;
  923. X        mid1 = m + vsf;
  924. X        mid2 = v - vsf;
  925. X        switch (sextant) {
  926. X            case 0: *r = v; *g = mid1; *b = m; break;
  927. X            case 1: *r = mid2; *g = v; *b = m; break;
  928. X            case 2: *r = m; *g = v; *b = mid1; break;
  929. X            case 3: *r = m; *g = mid2; *b = v; break;
  930. X            case 4: *r = mid1; *g = m; *b = v; break;
  931. X            case 5: *r = v; *g = m; *b = mid2; break;
  932. X        }
  933. X    }
  934. X}
  935. X
  936. X
  937. END_OF_FILE
  938. if test 1736 -ne `wc -c <'HSLtoRGB.c'`; then
  939.     echo shar: \"'HSLtoRGB.c'\" unpacked with wrong size!
  940. fi
  941. # end of 'HSLtoRGB.c'
  942. fi
  943. if test -f 'Hash3D.c' -a "${1}" != "-c" ; then 
  944.   echo shar: Will not clobber existing file \"'Hash3D.c'\"
  945. else
  946. echo shar: Extracting \"'Hash3D.c'\" \(1454 characters\)
  947. sed "s/^X//" >'Hash3D.c' <<'END_OF_FILE'
  948. X/*
  949. XA 3D Grid Hashing Frunction
  950. Xby Brian Wyvill
  951. Xfrom "Graphics Gems", Academic Press, 1990
  952. X*/
  953. X
  954. X/* Test Program for 3D hash function.
  955. XIn C the hash function can be defined in a macro which
  956. Xavoids a function call
  957. Xand the bit operations are defined in the language.
  958. X*/
  959. X
  960. X#include <stdio.h>
  961. X#include <math.h>
  962. X#include "GraphicsGems.h"        
  963. X
  964. X#define RANGE       256
  965. X#define NBITS       4
  966. X#define RBITS       4
  967. X#define MASK        0360
  968. X#define HASH(a,b,c) ((((a&MASK)<<NBITS|b&MASK)<<NBITS|c&MASK)>>RBITS)
  969. X#define HSIZE       1<<NBITS*3
  970. X#define IABS(x)     (int)((x) < 0 ? -(x) : (x))
  971. X
  972. Xtypedef struct {
  973. X  double x,y,z;
  974. X} Triple, *RefTriple;
  975. X
  976. Xtypedef struct {   /* linked list of objects to be stored */
  977. X  Triple origin;
  978. X  struct Object *link;
  979. X} Object, *RefObject;
  980. X
  981. Xtypedef struct {  /* linked list of voxels (object pointers) */
  982. X  RefObject objectList;
  983. X  struct Voxel *link;
  984. X} Voxel, *RefVoxel;
  985. X
  986. XRefVoxel table[HSIZE];  /* Table of pointers to Voxels */
  987. X
  988. X
  989. Xcheckrange(z) double z;
  990. X{
  991. X  if (z < 0 || z >= RANGE)
  992. X    fprintf(stderr,"%f out of range\n",z),         exit();
  993. X}
  994. X
  995. Xdouble getcoord()
  996. X{
  997. X  char buf[80];
  998. X  double z;
  999. X  scanf("%s",buf);
  1000. X  z = atof(buf);
  1001. X  checkrange(z);  
  1002. X  return z;
  1003. X}
  1004. X
  1005. Xmain()
  1006. X{
  1007. X  Triple a;
  1008. X  while (TRUE) {
  1009. X    printf("Enter object position x y z ===> ");
  1010. X    a.x = getcoord();
  1011. X    a.y = getcoord();
  1012. X    a.z = getcoord();
  1013. X    printf("\ncoord: %d %d %d Hashes to %d\n",IABS(a.x),IABS(a.y),IABS(a.z),
  1014. X       HASH(IABS(a.x), IABS(a.y), IABS(a.z) ));
  1015. X  };  
  1016. X}
  1017. END_OF_FILE
  1018. if test 1454 -ne `wc -c <'Hash3D.c'`; then
  1019.     echo shar: \"'Hash3D.c'\" unpacked with wrong size!
  1020. fi
  1021. # end of 'Hash3D.c'
  1022. fi
  1023. if test -f 'HypotApprox.c' -a "${1}" != "-c" ; then 
  1024.   echo shar: Will not clobber existing file \"'HypotApprox.c'\"
  1025. else
  1026. echo shar: Extracting \"'HypotApprox.c'\" \(1545 characters\)
  1027. sed "s/^X//" >'HypotApprox.c' <<'END_OF_FILE'
  1028. X/*
  1029. XA Fast Approximation to the Hypotenuse
  1030. Xby Alan Paeth
  1031. Xfrom "Graphics Gems", Academic Press, 1990
  1032. X*/
  1033. X
  1034. Xint idist(x1, y1, x2, y2)
  1035. X     int x1, y1, x2, y2;
  1036. X    {
  1037. X/*
  1038. X * gives approximate distance from (x1,y1) to (x2,y2)
  1039. X * with only overestimations, and then never by more
  1040. X * than (9/8) + one bit uncertainty.
  1041. X */
  1042. X    if ((x2 -= x1) < 0) x2 = -x2;
  1043. X    if ((y2 -= y1) < 0) y2 = -y2;
  1044. X    return (x2 + y2 - (((x2>y2) ? y2 : x2) >> 1) );
  1045. X    }
  1046. X
  1047. Xint PntOnCirc(xp, yp, xc, yc, r)
  1048. X    int xp, yp, xc, yc, r;
  1049. X    {
  1050. X/* returns true IFF a test point (xp, yp) is to within a
  1051. X * pixel of the circle of center (xc, yc) and radius r.
  1052. X * "d" is an approximate length to circle's center, with
  1053. X * 1.0*r < dist < 1.12*r < (9/8)*r used for coarse testing.
  1054. X * The 9/8 ratio suggests the code: (x)<<3 and ((x)<<3)-(x).
  1055. X * Variables xp, yp, r and d should be of 32-bit precision.
  1056. X *
  1057. X * Note: (9/8) forms a very tight, proper inner bound but
  1058. X * must be slackened by one pixel for the outside test (#2)
  1059. X * to account for the -1/2 pixel absolute error introduced
  1060. X * when "idist" halves an odd integer; else rough clipping
  1061. X * will trim occasional points on the circle's perimeter.
  1062. X */
  1063. X    int d = idist(xp, yp, xc, yc);
  1064. X    if (  r >      d)   return(0);    /* far-in  */
  1065. X    if (9*r < 8*(d-1))  return(0);    /* far-out */
  1066. X/* full test: r < hypot(xp-xc,yp-yc) < r+1 */
  1067. X    xp -= xc;
  1068. X    yp -= yc;
  1069. X    d = xp*xp + yp*yp;
  1070. X    if (d < r*r) return(0);          /* near-in */
  1071. X    r += 1;
  1072. X    if (d > r*r) return(0);          /* near-out */
  1073. X    return(1);                       /* WITHIN */
  1074. X    }
  1075. END_OF_FILE
  1076. if test 1545 -ne `wc -c <'HypotApprox.c'`; then
  1077.     echo shar: \"'HypotApprox.c'\" unpacked with wrong size!
  1078. fi
  1079. # end of 'HypotApprox.c'
  1080. fi
  1081. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  1082.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  1083. else
  1084. echo shar: Extracting \"'MANIFEST'\" \(4705 characters\)
  1085. sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  1086. X   File Name        Archive #    Description
  1087. X-----------------------------------------------------------
  1088. X 2DClip                     1    2D Clipping: A Vector-Based Approach
  1089. X 2DClip/Makefile            1    Makefile    
  1090. X 2DClip/bio.c               2    Basic operations
  1091. X 2DClip/box.h               1    BOX definition
  1092. X 2DClip/clip.c              2    Clipping routines
  1093. X 2DClip/cross.c             4    Intersection calculation    
  1094. X 2DClip/line.h              1    Major definitions    
  1095. X AALines                    1    Rendering Anti-Aliased Lines
  1096. X AALines/00README           1    Information about AALines Gem
  1097. X AALines/AALines.c          4    Code to render an anti-aliased line
  1098. X AALines/AALines.h          1    Symbols & globals
  1099. X AALines/AAMain.c           1    Calling routine
  1100. X AALines/AATables.c         4    Initialization of tables and frame buffer
  1101. X AALines/FastMatMul.c       5    Fast routines to multiply 4x4 matrices
  1102. X AALines/LongConst.h        1    High-precision constants
  1103. X AALines/Makefile           1    Makefile
  1104. X AALines/utah.c             3    Interface to Utah Raster Toolkit
  1105. X AALines/utah.h             1    Declarations for URT interface
  1106. X AAPolyScan.c               4    Fast Anti_aliasing Polygon Scan Conversion    
  1107. X Albers.c                   3    Albers Equal-Area Conic Map Projection
  1108. X BinRec.c                   1    Recording Animation in Binary Order    
  1109. X BoundSphere.c              3    An Efficient Bounding Sphere    
  1110. X BoxSphere.c                2    Box-Sphere Intersection Checking
  1111. X CircleRect.c               1    Fast Circle-Rectangle Intersection Checking
  1112. X ConcaveScan.c              4    Concave Polygon Scan Conversion
  1113. X DigitalLine.c              1    Digital Line Drawing
  1114. X Dissolve.c                 3    A Digital "Dissolve" Effect
  1115. X DoubleLine.c               3    Symmetric Double Step Line Algorithm
  1116. X FastJitter.c               1    Efficient Generation of Sampling Jitter
  1117. X FitCurves.c                5    Automatically Fit Digitized Curves
  1118. X FixedTrig.c                1    Fixed-Point Trig with CORDIC Iterations
  1119. X Forms.c                    4    Forms, Vectors, and Transformations
  1120. X GGVecLib.c                 5    2D and 3D Vector C Library    
  1121. X GraphicsGems.h             3    Graphics Gems header file    
  1122. X HSLtoRGB.c                 1    A Fast HSL-to-RGB Transform
  1123. X Hash3D.c                   1    3D Grid Hashing Function
  1124. X HypotApprox.c              1    A Fast Approximation to the Hypotenuse    
  1125. X Interleave.c               4    Bit Interleaving for Quad- or Octrees
  1126. X Label.c                    2    Nice Numbers for Graph Labels
  1127. X LineEdge.c                 3    Fast Line-Edge Intersections on a Uniform Grid
  1128. X MANIFEST                   1    This shipping list
  1129. X Makefile                   2    Makefile for the whole shebang
  1130. X MatrixInvert.c             3    Matrix Inversion    
  1131. X MatrixOrtho.c              1    Matrix Orthogonalization
  1132. X MatrixPost.c               2    Efficient Post-Concatenation of Trans. Matrices    
  1133. X Median.c                   2    Median Finding on a 3x3 Grid
  1134. X NearestPoint.c             5    Nearest-Point-On-Curve and Bezier Root-Finder
  1135. X OrderDither.c              2    Ordered Dithering
  1136. X PixelInteger.c             1    Proper Treatment of Pixels As Integers
  1137. X PntOnLine.c                2    A Fast 2D Point-On-Line Test
  1138. X PolyScan                   1    Convex Polygon Scan Conversion & Clipping
  1139. X PolyScan/Makefile          1    Makefile
  1140. X PolyScan/fancytest.c       2    Phong-shading a Texture mapping test
  1141. X PolyScan/poly.c            2    Simple utilities for polygon data structure    
  1142. X PolyScan/poly.h            2    Definitions for polygon package
  1143. X PolyScan/poly_clip.c       3    Homogeneous 3D polygon clipper
  1144. X PolyScan/poly_scan.c       3    Convex polygon point-sampled scan conversion
  1145. X PolyScan/scantest.c        2    Gouraud shading and Z-buffer demo    
  1146. X Quaternions.c              2    Using Quaternions for Coding 3D Transformations    
  1147. X README                     1    General information
  1148. X RGBTo4Bits.c               1    Mapping RGB Triples Onto Four Bits    
  1149. X RayBox.c                   1    Fast Ray-Box Intersection
  1150. X RayPolygon.c               1    An Efficient Ray-Polygon Intersection
  1151. X Roots3And4.c               3    Cubic and Quartic Roots
  1152. X SeedFill.c                 2    A Seed Fill Algorithm
  1153. X SquareRoot.c               2    A High-Speed, Low-Precision Square Root
  1154. X Sturm                      1    Using Sturm Sequences to Bracket Real Roots
  1155. X Sturm/Makefile             1    Makefile
  1156. X Sturm/main.c               2    Sample driver program
  1157. X Sturm/solve.h              1    Useful constants and types
  1158. X Sturm/sturm.c              4    Functions to build and evaluate Sturm sequence
  1159. X Sturm/util.c               1    Root polishing and evaluating utilities
  1160. X TransBox.c                 1    Transforming Axis-Aligned Bounding Boxes
  1161. X TriPoints.c                2    Generating Random Points in Triangles
  1162. X ViewTrans.c                1    3D Viewing and Rotation using Orthonormal Bases
  1163. END_OF_FILE
  1164. if test 4705 -ne `wc -c <'MANIFEST'`; then
  1165.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  1166. fi
  1167. # end of 'MANIFEST'
  1168. fi
  1169. if test -f 'MatrixOrtho.c' -a "${1}" != "-c" ; then 
  1170.   echo shar: Will not clobber existing file \"'MatrixOrtho.c'\"
  1171. else
  1172. echo shar: Extracting \"'MatrixOrtho.c'\" \(1335 characters\)
  1173. sed "s/^X//" >'MatrixOrtho.c' <<'END_OF_FILE'
  1174. X/* 
  1175. XMatrix Orthogonalization
  1176. XEric Raible
  1177. Xfrom "Graphics Gems", Academic Press, 1990
  1178. X*/
  1179. X
  1180. X/*
  1181. X * Reorthogonalize matrix R - that is find an orthogonal matrix that is
  1182. X * "close" to R by computing an approximation to the orthogonal matrix
  1183. X *
  1184. X *           T  -1/2
  1185. X *   RC = R(R R)
  1186. X *                                 T      -1
  1187. X * [RC is orthogonal because (RC) = (RC) ]
  1188. X *                                                       -1/2
  1189. X * To compute C, we evaluate the Taylor expansion of F(x) = (I + x)
  1190. X * (where x = C - I) about x=0.
  1191. X * This gives C = I - (1/2)x + (3/8)x^2 - (5/16)x^3 + ...
  1192. X */
  1193. X
  1194. X#include "GraphicsGems.h"
  1195. X
  1196. Xstatic float coef[10] =             /* From mathematica */
  1197. X  { 1, -1/2., 3/8., -5/16., 35/128., -63/256.,
  1198. X    231/1024., -429/2048., 6435/32768., -12155/65536. };
  1199. X
  1200. XMATRIX_reorthogonalize (R, limit)
  1201. X     Matrix R;
  1202. X{
  1203. X  Matrix I, Temp, X, X_power, Sum;
  1204. X  int power;
  1205. X
  1206. X  limit = MAX(limit, 10);
  1207. X
  1208. X  MATRIX_transpose (R, Temp);        /* Rt */
  1209. X  MATRIX_multiply (Temp, R, Temp);    /* RtR */
  1210. X  MATRIX_identify (I);
  1211. X  MATRIX_subtract (Temp, I, X);    /* RtR - I */
  1212. X  MATRIX_identify (X_power);        /* X^0 */
  1213. X  MATRIX_identify (Sum);            /* coef[0] * X^0 */
  1214. X
  1215. X  for (power = 1; power < limit; ++power)
  1216. X    {
  1217. X      MATRIX_multiply (X_power, X, X_power);
  1218. X      MATRIX_constant_multiply (coef[power], X_power, Temp);
  1219. X      MATRIX_add (Sum, Temp, Sum);
  1220. X    }
  1221. X
  1222. X  MATRIX_multiply (R, Sum, R);
  1223. X}
  1224. END_OF_FILE
  1225. if test 1335 -ne `wc -c <'MatrixOrtho.c'`; then
  1226.     echo shar: \"'MatrixOrtho.c'\" unpacked with wrong size!
  1227. fi
  1228. # end of 'MatrixOrtho.c'
  1229. fi
  1230. if test -f 'PixelInteger.c' -a "${1}" != "-c" ; then 
  1231.   echo shar: Will not clobber existing file \"'PixelInteger.c'\"
  1232. else
  1233. echo shar: Extracting \"'PixelInteger.c'\" \(1534 characters\)
  1234. sed "s/^X//" >'PixelInteger.c' <<'END_OF_FILE'
  1235. X/*
  1236. XProper Treatment of Pixels as Integers
  1237. Xby Alan Paeth
  1238. Xfrom "Graphics Gems", Academic Press, 1990
  1239. X*/
  1240. X
  1241. X#define Min     code[2]
  1242. X#define Med     code[1]
  1243. X#define Max     code[0]
  1244. X#define NCODE   3
  1245. X
  1246. X/*
  1247. X * A call to getplanes of the form:
  1248. X * getplanes(&red, &green, &blue, 256, "grb");
  1249. X *
  1250. X * fills the first three integer pointers with (near) identical
  1251. X * values which maximize red*green*blue <= 256. The final parameter
  1252. X * string defines tie-break order, here green>=red>=blue (the usual
  1253. X * default). The present code procedure calls "err(string, arg)"
  1254. X * given bad parameters; it is a simple task to rewrite the code as
  1255. X * a function which returns a success/failure code(s), as needed.
  1256. X *
  1257. X * In the example given above the code fills in the values
  1258. X * red = 6, green = 7, blue = 6.
  1259. X */
  1260. X
  1261. Xgetplanes(r, g, b, n, bias)
  1262. X    int *r, *g, *b;
  1263. X    char *bias;
  1264. X    {
  1265. X    int i, code[NCODE];
  1266. X    if(strlen(bias) != NCODE )
  1267. X        err("bias string \"%s\" wrong length",bias);
  1268. X    Min = Med = Max = 0;
  1269. X    *r = *g = *b = 0;
  1270. X    while(Min*Min*Min <= n) Min++;
  1271. X    Min--;
  1272. X    while(Med*Med*Min <= n) Med++;
  1273. X    Med--;
  1274. X    Max = n/(Min*Med);
  1275. X    for( i = 0; i < NCODE; i++ )
  1276. X       {
  1277. X        switch(bias[i])
  1278. X            {
  1279. X            case 'r': case 'R': *r = code[i]; break;
  1280. X            case 'g': case 'G': *g = code[i]; break;
  1281. X            case 'b': case 'B': *b = code[i]; break;
  1282. X            default: err("bad bias character: \'%c\'",bias[i]); break;
  1283. X            }
  1284. X        }
  1285. X    if (!(*r && *g && *b)) err("bias string \"%s\" deficient", bias);
  1286. X    }
  1287. X    
  1288. END_OF_FILE
  1289. if test 1534 -ne `wc -c <'PixelInteger.c'`; then
  1290.     echo shar: \"'PixelInteger.c'\" unpacked with wrong size!
  1291. fi
  1292. # end of 'PixelInteger.c'
  1293. fi
  1294. if test ! -d 'PolyScan' ; then
  1295.     echo shar: Creating directory \"'PolyScan'\"
  1296.     mkdir 'PolyScan'
  1297. fi
  1298. if test -f 'PolyScan/Makefile' -a "${1}" != "-c" ; then 
  1299.   echo shar: Will not clobber existing file \"'PolyScan/Makefile'\"
  1300. else
  1301. echo shar: Extracting \"'PolyScan/Makefile'\" \(380 characters\)
  1302. sed "s/^X//" >'PolyScan/Makefile' <<'END_OF_FILE'
  1303. X# Makefile for scantest, test program for generic convex polygon scan conversion
  1304. X#
  1305. X# Note: fancytest.c needs a main routine and several auxiliary routines
  1306. X# in order to be compiled.
  1307. X
  1308. XCFLAGS = $(GENCFLAGS)
  1309. X
  1310. Xscantest: scantest.o poly_scan.o poly.o
  1311. X    $(CC) $(CFLAGS) -o scantest scantest.o poly_scan.o poly.o -lm
  1312. X
  1313. Xclean:
  1314. X    /bin/rm -f scantest.o poly_clip.o poly_scan.o poly.o scantest
  1315. END_OF_FILE
  1316. if test 380 -ne `wc -c <'PolyScan/Makefile'`; then
  1317.     echo shar: \"'PolyScan/Makefile'\" unpacked with wrong size!
  1318. fi
  1319. # end of 'PolyScan/Makefile'
  1320. fi
  1321. if test -f 'README' -a "${1}" != "-c" ; then 
  1322.   echo shar: Will not clobber existing file \"'README'\"
  1323. else
  1324. echo shar: Extracting \"'README'\" \(5838 characters\)
  1325. sed "s/^X//" >'README' <<'END_OF_FILE'
  1326. X[ This package last updated Fri Oct 12 15:53:09 EDT 1990. ]
  1327. X
  1328. XREADME
  1329. X
  1330. XThis package contains the most up-to-date versions of the C source
  1331. Xfiles from the book "Graphics Gems" (Editor Andrew S. Glassner,
  1332. XAcademic Press, 1990 ISBM 0-12-286165-5, 833 pgs.).
  1333. X
  1334. XAll known bugs have been fixed, formatting problems have been
  1335. Xcorrected, and enchancements to some of the original Gems have
  1336. Xbeen made.
  1337. X
  1338. XMakefiles are provided that create stand-alone programs, many object
  1339. Xfiles, and "gemlib.a".  This Graphics Gem Library is created for
  1340. Xcompilation-testing only, and is not intended to be a usable library
  1341. X(although it may become so in the future).  Indeed, many of the Gems
  1342. Xwill not compile or run properly without the addition of functions,
  1343. Xtables, and the like.  See the book and the Makefile for more details.
  1344. X
  1345. XEach Gem is made available on an as-is basis; although 
  1346. Xconsiderable effort has been expended to check the programs 
  1347. Xas originally designed and their release in electronic form, 
  1348. Xthe authors and the publisher make no guarantees or 
  1349. Xwarrantees about the correctness of any of these programs or 
  1350. Xalgorithms.  
  1351. X
  1352. XThe authors and the publisher hold no copyright restrictions 
  1353. Xon any of these files; this source code is public domain, and 
  1354. Xis freely available to the entire computer graphics community 
  1355. Xfor study, use, and modification.  We do request that the 
  1356. Xcomment at the top of each file, identifying the original 
  1357. Xauthor and its original publication in the book Graphics 
  1358. XGems, be retained in all programs that use these files.
  1359. X
  1360. XAn archive of the most current versions of all the Gems is maintained
  1361. Xon weedeater.math.yale.edu (130.132.23.17) and is available via
  1362. Xanonymous ftp in pub/GraphicsGems/src.
  1363. X
  1364. XYou are encouraged to submit bug fixes, skeleton programs, and the like
  1365. Xto Craig Kolb (kolb@yale.edu).
  1366. X
  1367. XAndrew Glassner / Craig Kolb
  1368. X
  1369. X================
  1370. X
  1371. XThe table below gives the correspondence between each source
  1372. Xfile in this directory and the name of the Gem it implements.
  1373. XEach implementation illustrates one way to realize the
  1374. Xtechniques described by the accompanying Gem in the book. 
  1375. XThe files here contain only the source code for that 
  1376. Xrealization.  For a more complete description of the 
  1377. Xalgorithms and their applications see the Gems of the same 
  1378. Xname in the first 11 Chapters of the book.
  1379. X
  1380. X---------- header files ----------
  1381. XGraphicsGems.h         / Graphics Gems C Header File
  1382. X
  1383. X----------    C code    ----------
  1384. X2DClip/*               / Two-Dimensional Clipping: 
  1385. X                         A Vector-Based Approach
  1386. XAALines/*              / Rendering Anti-Aliased Lines
  1387. XAAPolyScan.c           / Fast Anti-Aliasing Polygon
  1388. X                         Scan Conversion
  1389. XAlbers.c               / Albers Equal-Area Conic Map
  1390. X                         Projection
  1391. XBinRec.c               / Recording Animation in Binary Order 
  1392. X                         For Progressive Temporal Refinement  
  1393. XBoundSphere.c          / An Efficient Bounding Sphere
  1394. XBoxSphere.c            / A Simple Method for Box-Sphere 
  1395. X                         Intersection Checking 
  1396. XCircleRect.c           / Fast Circle-Rectangle Intersection
  1397. X                         Checking 
  1398. XConcaveScan.c          / Concave Polygon Scan Conversion
  1399. XDigitalLine.c          / Digital Line Drawing
  1400. XDissolve.c            / A Digital "Dissolve" Effect
  1401. XDoubleLine.c           / Symmetric Double Step Line Algorithm
  1402. XFastJitter.c           / Efficient Generation of Sampling
  1403. X                         Jitter Using Look-up Tables
  1404. XFitCurves.c            / An Algorithm for Automatically 
  1405. X                         Fitting Digitized Curves
  1406. XFixedTrig.c            / Fixed-Point Trigonometry with 
  1407. X                         CORDIC Iterations
  1408. XForms.c                / Forms, Vectors, and Transforms
  1409. XGGVecLib.c             / 2D And 3D Vector C Library   
  1410. XHSLtoRGB.c             / A Fast HSL-to-RGB Transform
  1411. XHash3D.c               / 3D Grid Hashing Function
  1412. XHypotApprox.c          / A Fast Approximation to 
  1413. X                         the Hypotenuse
  1414. XInterleave.c           / Bit Interleaving for Quad- 
  1415. X                         or Octrees
  1416. XLabel.c                / Nice Numbers for Graph Labels
  1417. XLineEdge.c             / Fast Line-Edge Intersections On 
  1418. X                         A Uniform Grid   
  1419. XMatrixInvert.c         / Matrix Inversion
  1420. XMatrixOrtho.c          / Matrix Orthogonalization
  1421. XMatrixPost.c           / Efficient Post-Concatenation of 
  1422. X                         Transformation Matrices
  1423. XMedian.c               / Median Finding on a 3x3 Grid
  1424. XNearestPoint.c         / Solving the 
  1425. X                         Nearest-Point-On-Curve Problem
  1426. X                         and
  1427. X                         A Bezier Curve-Based Root-Finder
  1428. XOrderDither.c          / Ordered Dithering
  1429. XPixelInteger.c         / Proper Treatment of Pixels 
  1430. X                         As Integers
  1431. XPntOnLine.c            / A Fast 2D Point-On-Line Test
  1432. XPolyScan/*             / Generic Convex Polygon 
  1433. X                         Scan Conversion and Clipping
  1434. XQuaternions.c          / Using Quaternions for Coding 
  1435. X                         3D Transformations
  1436. XRGBTo4Bits.c           / Mapping RGB Triples Onto Four Bits
  1437. XRayBox.c               / Fast Ray-Box Intersection
  1438. XRayPolygon.c           / An Efficient Ray-Polygon 
  1439. X                         Intersection
  1440. XRoots3And4.c           / Cubic and Quartic Roots
  1441. XSeedFill.c             / A Seed Fill Algorithm
  1442. XSquareRoot.c           / A High-Speed, Low-Precision 
  1443. X                         Square Root
  1444. XSturm/*                / Using Sturm Sequences to Bracket 
  1445. X                         Real Roots of Polynomial Equations
  1446. XTransBox.c             / Transforming Axis-Aligned 
  1447. X                         Bounding Boxes
  1448. XTriPoints.c            / Generating Random Points 
  1449. X                         In Triangles   
  1450. XViewTrans.c            / 3D Viewing and Rotation Using
  1451. X                         Orthonormal Bases
  1452. END_OF_FILE
  1453. if test 5838 -ne `wc -c <'README'`; then
  1454.     echo shar: \"'README'\" unpacked with wrong size!
  1455. fi
  1456. # end of 'README'
  1457. fi
  1458. if test -f 'RGBTo4Bits.c' -a "${1}" != "-c" ; then 
  1459.   echo shar: Will not clobber existing file \"'RGBTo4Bits.c'\"
  1460. else
  1461. echo shar: Extracting \"'RGBTo4Bits.c'\" \(1448 characters\)
  1462. sed "s/^X//" >'RGBTo4Bits.c' <<'END_OF_FILE'
  1463. X/*
  1464. XMapping RGB Triples onto Four Bits
  1465. Xby Alan Paeth
  1466. Xfrom "Graphics Gems", Academic Press, 1990
  1467. X*/
  1468. X
  1469. Xremap8(R, G, B, R2, G2, B2)
  1470. X    float R, G, B, *R2, *G2, *B2;
  1471. X    {
  1472. X/*
  1473. X * remap8 maps floating (R,G,B) triples onto quantized
  1474. X * (R2,B2,B2) triples and returns the code (vertex)
  1475. X * value/color table entry for the quantization. The
  1476. X * points (eight) are the vertices of the cube.
  1477. X */
  1478. X    int code;
  1479. X    *R2 = *G2 = *B2 = 0.0;
  1480. X    code = 0;
  1481. X    if (R >= 0.5) { *R2 = 1.0; code |= 1; }
  1482. X    if (G >= 0.5) { *G2 = 1.0; code |= 2; }
  1483. X    if (B >= 0.5) { *B2 = 1.0; code |= 4; }
  1484. X    return(code);
  1485. X    }
  1486. X
  1487. X/*
  1488. X * remap14 maps floating (R,G,B) triples onto quantized
  1489. X * (R2,B2,B2) triples and returns the code (vertex)
  1490. X * value/color table entry for the quantization. The
  1491. X * points (fourteen) are the vertices of the cuboctahedron.
  1492. X */
  1493. X
  1494. Xfloat rval[] = { 0.,.5 ,.5 , 1.,.0 , 0., 0.,.5,
  1495. X                .5 , 1., 1., 1., 0.,.5 ,.5 , 1.};
  1496. Xfloat gval[] = { 0.,.5 , 0., 0.,.5 , 1., 0.,.5,
  1497. X                .5 , 1., 0.,.5 , 1., 1.,.5 , 1.};
  1498. Xfloat bval[] = { 0., 0.,.5 , 0.,.5 , 0., 1.,.5,
  1499. X                .5 , 0., 1.,.5 , 1.,.5 , 1., 1.};
  1500. X
  1501. Xint remap14(R, G, B,  R2, G2, B2)
  1502. X    float R, G, B, *R2, *G2, *B2;
  1503. X    {
  1504. X    int code = 0;
  1505. X    if ( R + G + B > 1.5) code |= 8;
  1506. X    if (-R + G + B > 0.5) code |= 4;
  1507. X    if ( R - G + B > 0.5) code |= 2;
  1508. X    if ( R + G - B > 0.5) code |= 1;
  1509. X    *R2 = rval[code];
  1510. X    *G2 = gval[code];
  1511. X    *B2 = bval[code];
  1512. X    return(code);
  1513. X    }
  1514. END_OF_FILE
  1515. if test 1448 -ne `wc -c <'RGBTo4Bits.c'`; then
  1516.     echo shar: \"'RGBTo4Bits.c'\" unpacked with wrong size!
  1517. fi
  1518. # end of 'RGBTo4Bits.c'
  1519. fi
  1520. if test -f 'RayBox.c' -a "${1}" != "-c" ; then 
  1521.   echo shar: Will not clobber existing file \"'RayBox.c'\"
  1522. else
  1523. echo shar: Extracting \"'RayBox.c'\" \(1793 characters\)
  1524. sed "s/^X//" >'RayBox.c' <<'END_OF_FILE'
  1525. X/* 
  1526. XFast Ray-Box Intersection
  1527. Xby Andrew Woo
  1528. Xfrom "Graphics Gems", Academic Press, 1990
  1529. X*/
  1530. X
  1531. X#include "GraphicsGems.h"
  1532. X
  1533. X#define NUMDIM    3
  1534. X#define RIGHT    0
  1535. X#define LEFT    1
  1536. X#define MIDDLE    2
  1537. X
  1538. Xchar HitBoundingBox(minB,maxB, origin, dir,coord)
  1539. Xdouble minB[NUMDIM], maxB[NUMDIM];        /*box */
  1540. Xdouble origin[NUMDIM], dir[NUMDIM];        /*ray */
  1541. Xdouble coord[NUMDIM];                /* hit point */
  1542. X{
  1543. X    char inside = TRUE;
  1544. X    char quadrant[NUMDIM];
  1545. X    register int i;
  1546. X    int whichPlane;
  1547. X    double maxT[NUMDIM];
  1548. X    double candidatePlane[NUMDIM];
  1549. X
  1550. X    /* Find candidate planes; this loop can be avoided if
  1551. X       rays cast all from the eye(assume perpsective view) */
  1552. X    for (i=0; i<NUMDIM; i++)
  1553. X        if(origin[i] < minB[i]) {
  1554. X            quadrant[i] = LEFT;
  1555. X            candidatePlane[i] = minB[i];
  1556. X            inside = FALSE;
  1557. X        }else if (origin[i] > maxB[i]) {
  1558. X            quadrant[i] = RIGHT;
  1559. X            candidatePlane[i] = maxB[i];
  1560. X            inside = FALSE;
  1561. X        }else    {
  1562. X            quadrant[i] = MIDDLE;
  1563. X        }
  1564. X
  1565. X    /* Ray origin inside bounding box */
  1566. X    if(inside)    {
  1567. X        coord = origin;
  1568. X        return (TRUE);
  1569. X    }
  1570. X
  1571. X
  1572. X    /* Calculate T distances to candidate planes */
  1573. X    for (i = 0; i < NUMDIM; i++)
  1574. X        if (quadrant[i] != MIDDLE && dir[i] !=0.)
  1575. X            maxT[i] = (candidatePlane[i]-origin[i]) / dir[i];
  1576. X        else
  1577. X            maxT[i] = -1.;
  1578. X
  1579. X    /* Get largest of the maxT's for final choice of intersection */
  1580. X    whichPlane = 0;
  1581. X    for (i = 1; i < NUMDIM; i++)
  1582. X        if (maxT[whichPlane] < maxT[i])
  1583. X            whichPlane = i;
  1584. X
  1585. X    /* Check final candidate actually inside box */
  1586. X    if (maxT[whichPlane] < 0.) return (FALSE);
  1587. X    for (i = 0; i < NUMDIM; i++)
  1588. X        if (whichPlane != i) {
  1589. X            coord[i] = origin[i] + maxT[whichPlane] *dir[i];
  1590. X            if ((quadrant[i] == RIGHT && coord[i] < minB[i]) ||
  1591. X               (quadrant[i] == LEFT && coord[i] > maxB[i]))
  1592. X               return (FALSE);    /* outside box */
  1593. X            }else {
  1594. X                coord[i] = candidatePlane[i];
  1595. X            }
  1596. X    return (TRUE);                /* ray hits box */
  1597. X}    
  1598. X
  1599. END_OF_FILE
  1600. if test 1793 -ne `wc -c <'RayBox.c'`; then
  1601.     echo shar: \"'RayBox.c'\" unpacked with wrong size!
  1602. fi
  1603. # end of 'RayBox.c'
  1604. fi
  1605. if test -f 'RayPolygon.c' -a "${1}" != "-c" ; then 
  1606.   echo shar: Will not clobber existing file \"'RayPolygon.c'\"
  1607. else
  1608. echo shar: Extracting \"'RayPolygon.c'\" \(1553 characters\)
  1609. sed "s/^X//" >'RayPolygon.c' <<'END_OF_FILE'
  1610. X/*
  1611. XAn Efficient Ray/Polygon Intersection
  1612. Xby Didier Badouel
  1613. Xfrom "Graphics Gems", Academic Press, 1990
  1614. X*/
  1615. X
  1616. X/* the value of t is computed.
  1617. X * i1 and i2 come from the polygon description.
  1618. X * V is the vertex table for the polygon and N the
  1619. X * associated normal vectors.
  1620. X */
  1621. XP[0] = ray.O[0] + ray.D[0]*t;
  1622. XP[1] = ray.O[1] + ray.D[1]*t;
  1623. XP[2] = ray.O[2] + ray.D[2]*t;
  1624. Xu0 = P[i1] - V[0][i1]; v0 = P[i2] - V[0][i2];
  1625. Xinter = FALSE; i = 2;
  1626. Xdo {
  1627. X    /* The polygon is viewed as (n-2) triangles. */
  1628. X    u1 = V[i-1][i1] - V[0][i1]; v1 = V[i-1][i2] - V[0][i2];
  1629. X    u2 = V[i  ][i1] - V[0][i1]; v2 = V[i  ][i2] - V[0][i2];
  1630. X
  1631. X    if (u1 == 0)    {
  1632. X        beta = u0/u2;
  1633. X        if ((beta >= 0.)&&(beta <= 1.)) {
  1634. X            alpha = (v0 - beta*v2)/v1;
  1635. X            inter = ((alpha >= 0.)&&(alpha+beta) <= 1.));
  1636. X        }
  1637. X    } else {
  1638. X        beta = (v0*u1 - u0*v1)/(v2*u1 - u2*v1);
  1639. X        if ((beta >= 0.)&&(beta <= 1.)) {
  1640. X            alpha = (u0 - beta*u2)/u1;
  1641. X            inter = ((alpha >= 0)&&((alpha+beta) <= 1.));
  1642. X        }
  1643. X    }
  1644. X} while ((!inter)&&(++i < poly.n));
  1645. X
  1646. Xif (inter) {
  1647. X    /* Storing the intersection point. */
  1648. X    ray.P[0] = P[0]; ray.P[1] = P[1]; ray.P[2] = P[2];
  1649. X    /* the normal vector can be interpolated now or later. */
  1650. X    if (poly.interpolate) {
  1651. X        gamma = 1 - (alpha+beta);
  1652. X        ray.normal[0] = gamma * N[0][0] + alpha * N[i-1][0] + 
  1653. X        beta * N[i][0];
  1654. X        ray.normal[1] = gamma * N[0][1] + alpha * N[i-1][1] +
  1655. X         beta * N[i][1];
  1656. X        ray.normal[2] = gamma * N[0][2] + alpha * N[i-1][2] +
  1657. X         beta * N[i][2];
  1658. X    }
  1659. X}
  1660. Xreturn (inter);
  1661. END_OF_FILE
  1662. if test 1553 -ne `wc -c <'RayPolygon.c'`; then
  1663.     echo shar: \"'RayPolygon.c'\" unpacked with wrong size!
  1664. fi
  1665. # end of 'RayPolygon.c'
  1666. fi
  1667. if test ! -d 'Sturm' ; then
  1668.     echo shar: Creating directory \"'Sturm'\"
  1669.     mkdir 'Sturm'
  1670. fi
  1671. if test -f 'Sturm/Makefile' -a "${1}" != "-c" ; then 
  1672.   echo shar: Will not clobber existing file \"'Sturm/Makefile'\"
  1673. else
  1674. echo shar: Extracting \"'Sturm/Makefile'\" \(211 characters\)
  1675. sed "s/^X//" >'Sturm/Makefile' <<'END_OF_FILE'
  1676. X#
  1677. X# Makefile
  1678. X#
  1679. X#    command file for make to compile the solver.
  1680. X
  1681. Xsolve: main.o sturm.o util.o
  1682. X    cc -o solve main.o sturm.o util.o -lm
  1683. X
  1684. Xclean:
  1685. X    /bin/rm -f main.o sturm.o util.o solve
  1686. X
  1687. Xmain.o sturm.o util.o: solve.h
  1688. END_OF_FILE
  1689. if test 211 -ne `wc -c <'Sturm/Makefile'`; then
  1690.     echo shar: \"'Sturm/Makefile'\" unpacked with wrong size!
  1691. fi
  1692. # end of 'Sturm/Makefile'
  1693. fi
  1694. if test -f 'Sturm/solve.h' -a "${1}" != "-c" ; then 
  1695.   echo shar: Will not clobber existing file \"'Sturm/solve.h'\"
  1696. else
  1697. echo shar: Extracting \"'Sturm/solve.h'\" \(710 characters\)
  1698. sed "s/^X//" >'Sturm/solve.h' <<'END_OF_FILE'
  1699. X
  1700. X/*
  1701. X * solve.h
  1702. X *
  1703. X *    some useful constants and types.
  1704. X */
  1705. X#define         MAX_ORDER          12    
  1706. X/* maximum order for a polynomial */
  1707. X    
  1708. X#define          RELERROR              1.0e-14
  1709. X/* smallest relative error we want */
  1710. X
  1711. X#define          MAXPOW            32        
  1712. X/* max power of 10 we wish to search to */
  1713. X
  1714. X#define          MAXIT             800        
  1715. X/* max number of iterations */
  1716. X
  1717. X/* a coefficient smaller than SMALL_ENOUGH is considered to 
  1718. X   be zero (0.0). */
  1719. X
  1720. X#define          SMALL_ENOUGH        1.0e-12
  1721. X
  1722. X
  1723. X/*
  1724. X * structure type for representing a polynomial
  1725. X */
  1726. Xtypedef      struct    p {
  1727. X             int    ord;
  1728. X             double    coef[MAX_ORDER];
  1729. X} poly;
  1730. X
  1731. Xextern     int        modrf();
  1732. Xextern     int        numroots();
  1733. Xextern     int        numchanges();
  1734. Xextern     int        buildsturm();
  1735. X
  1736. Xextern     double    evalpoly();
  1737. X    
  1738. X
  1739. END_OF_FILE
  1740. if test 710 -ne `wc -c <'Sturm/solve.h'`; then
  1741.     echo shar: \"'Sturm/solve.h'\" unpacked with wrong size!
  1742. fi
  1743. # end of 'Sturm/solve.h'
  1744. fi
  1745. if test -f 'Sturm/util.c' -a "${1}" != "-c" ; then 
  1746.   echo shar: Will not clobber existing file \"'Sturm/util.c'\"
  1747. else
  1748. echo shar: Extracting \"'Sturm/util.c'\" \(1768 characters\)
  1749. sed "s/^X//" >'Sturm/util.c' <<'END_OF_FILE'
  1750. X
  1751. X/*
  1752. X * util.c
  1753. X *
  1754. X *    some utlity functions for root polishing and evaluating
  1755. X * polynomials.
  1756. X */
  1757. X#include <math.h>
  1758. X#include <stdio.h>
  1759. X#include "solve.h"
  1760. X
  1761. X/*
  1762. X * evalpoly
  1763. X *
  1764. X *    evaluate polynomial defined in coef returning its value.
  1765. X */
  1766. Xdouble
  1767. Xevalpoly (ord, coef, x)
  1768. X    int        ord;
  1769. X    double    *coef, x;
  1770. X{
  1771. X    double    *fp, f;
  1772. X
  1773. X    fp = &coef[ord];
  1774. X    f = *fp;
  1775. X
  1776. X    for (fp--; fp >= coef; fp--)
  1777. X    f = x * f + *fp;
  1778. X
  1779. X    return(f);
  1780. X}
  1781. X
  1782. X
  1783. X/*
  1784. X * modrf
  1785. X *
  1786. X *    uses the modified regula-falsi method to evaluate the root
  1787. X * in interval [a,b] of the polynomial described in coef. The
  1788. X * root is returned is returned in *val. The routine returns zero
  1789. X * if it can't converge.
  1790. X */
  1791. Xint
  1792. Xmodrf(ord, coef, a, b, val)
  1793. X    int        ord;
  1794. X    double    *coef;
  1795. X    double    a, b, *val;
  1796. X{
  1797. X    int        its;
  1798. X    double    fa, fb, x, lx, fx, lfx;
  1799. X    double    *fp, *scoef, *ecoef;
  1800. X
  1801. X    scoef = coef;
  1802. X    ecoef = &coef[ord];
  1803. X
  1804. X    fb = fa = *ecoef;
  1805. X    for (fp = ecoef - 1; fp >= scoef; fp--) {
  1806. X        fa = a * fa + *fp;
  1807. X        fb = b * fb + *fp;
  1808. X    }
  1809. X
  1810. X    /*
  1811. X     * if there is no sign difference the method won't work
  1812. X     */
  1813. X    if (fa * fb > 0.0)
  1814. X        return(0);
  1815. X
  1816. X    if (fabs(fa) < RELERROR) {
  1817. X        *val = a;
  1818. X        return(1);
  1819. X    }
  1820. X
  1821. X    if (fabs(fb) < RELERROR) {
  1822. X        *val = b;
  1823. X        return(1);
  1824. X    }
  1825. X
  1826. X    lfx = fa;
  1827. X    lx = a;
  1828. X
  1829. X
  1830. X    for (its = 0; its < MAXIT; its++) {
  1831. X
  1832. X        x = (fb * a - fa * b) / (fb - fa);
  1833. X
  1834. X        fx = *ecoef;
  1835. X        for (fp = ecoef - 1; fp >= scoef; fp--)
  1836. X                fx = x * fx + *fp;
  1837. X
  1838. X        if (fabs(x) > RELERROR) {
  1839. X                if (fabs(fx / x) < RELERROR) {
  1840. X                    *val = x;
  1841. X                    return(1);
  1842. X                }
  1843. X        } else if (fabs(fx) < RELERROR) {
  1844. X                *val = x;
  1845. X                return(1);
  1846. X        }
  1847. X
  1848. X        if ((fa * fx) < 0) {
  1849. X                b = x;
  1850. X                fb = fx;
  1851. X                if ((lfx * fx) > 0)
  1852. X                    fa /= 2;
  1853. X        } else {
  1854. X                a = x;
  1855. X                fa = fx;
  1856. X                if ((lfx * fx) > 0)
  1857. X                    fb /= 2;
  1858. X        }
  1859. X
  1860. X        lx = x;
  1861. X        lfx = fx;
  1862. X    }
  1863. X
  1864. X    fprintf(stderr, "modrf overflow %f %f %f\n", a, b, fx);
  1865. X
  1866. X    return(0);
  1867. X}
  1868. X    
  1869. X
  1870. X
  1871. END_OF_FILE
  1872. if test 1768 -ne `wc -c <'Sturm/util.c'`; then
  1873.     echo shar: \"'Sturm/util.c'\" unpacked with wrong size!
  1874. fi
  1875. # end of 'Sturm/util.c'
  1876. fi
  1877. if test -f 'TransBox.c' -a "${1}" != "-c" ; then 
  1878.   echo shar: Will not clobber existing file \"'TransBox.c'\"
  1879. else
  1880. echo shar: Extracting \"'TransBox.c'\" \(1672 characters\)
  1881. sed "s/^X//" >'TransBox.c' <<'END_OF_FILE'
  1882. X/*
  1883. XTransforming Axis-Aligned Bounding Boxes
  1884. Xby Jim Arvo
  1885. Xfrom "Graphics Gems", Academic Press, 1990
  1886. X*/
  1887. X
  1888. X#include "GraphicsGems.h"                                     
  1889. X/* Transforms a 3D axis-aligned box via a 3x3 matrix and a translation
  1890. X * vector and returns an axis-aligned box enclosing the result. */ 
  1891. X
  1892. Xvoid Transform_Box( M, T, A, B )
  1893. XMatrix3  M;      /* Transform matrix.             */
  1894. XVector3  T;      /* Translation matrix.           */
  1895. XBox3     A;      /* The original bounding box.    */
  1896. XBox3    *B;      /* The transformed bounding box. */
  1897. X    {
  1898. X    float  a, b;
  1899. X    float  Amin[3], Amax[3];
  1900. X    float  Bmin[3], Bmax[3];
  1901. X    int    i, j;
  1902. X
  1903. X    /*Copy box A into a min array and a max array for easy reference.*/
  1904. X
  1905. X    Amin[0] = A.min.x;  Amax[0] = A.max.x;
  1906. X    Amin[1] = A.min.y;  Amax[1] = A.max.y;
  1907. X    Amin[2] = A.min.z;  Amax[2] = A.max.z;
  1908. X
  1909. X    /* Take care of translation by beginning at T. */
  1910. X
  1911. X    Bmin[0] = Bmax[0] = T.x;
  1912. X    Bmin[1] = Bmax[1] = T.y;
  1913. X    Bmin[2] = Bmax[2] = T.z;
  1914. X
  1915. X    /* Now find the extreme points by considering the product of the */
  1916. X    /* min and max with each component of M.  */
  1917. X                     
  1918. X    for( i = 0; i < 3; i++ )
  1919. X    for( j = 0; j < 3; j++ )
  1920. X        {
  1921. X        a = M.element[i][j] * Amin[j];
  1922. X        b = M.element[i][j] * Amax[j];
  1923. X        if( a < b ) 
  1924. X
  1925. X            { 
  1926. X            Bmin[i] += a; 
  1927. X            Bmax[i] += b;
  1928. X            }
  1929. X        else 
  1930. X            { 
  1931. X            Bmin[i] += b; 
  1932. X            Bmax[i] += a;
  1933. X            }
  1934. X        }
  1935. X
  1936. X    /* Copy the result into the new box. */
  1937. X
  1938. X    B->min.x = Bmin[0];  B->max.x = Bmax[0];
  1939. X    B->min.y = Bmin[1];  B->max.y = Bmax[1];
  1940. X    B->min.z = Bmin[2];  B->max.z = Bmax[2];
  1941. X
  1942. X    } 
  1943. END_OF_FILE
  1944. if test 1672 -ne `wc -c <'TransBox.c'`; then
  1945.     echo shar: \"'TransBox.c'\" unpacked with wrong size!
  1946. fi
  1947. # end of 'TransBox.c'
  1948. fi
  1949. if test -f 'ViewTrans.c' -a "${1}" != "-c" ; then 
  1950.   echo shar: Will not clobber existing file \"'ViewTrans.c'\"
  1951. else
  1952. echo shar: Extracting \"'ViewTrans.c'\" \(1575 characters\)
  1953. sed "s/^X//" >'ViewTrans.c' <<'END_OF_FILE'
  1954. X/*
  1955. X3D Viewing and Rotation Using Orthonormal Bases
  1956. Xby Steve Cunningham
  1957. Xfrom "Grahics Gems", Academic Press, 1990
  1958. X*/
  1959. X
  1960. X/*
  1961. X * Transformations are presented as 4 by 3 matrices, omitting the
  1962. X * fourth column to save memory.
  1963. X *
  1964. X * Functions are used from the Graphics Gems vector C library
  1965. X */
  1966. X
  1967. X
  1968. X#include "GraphicsGems.h"        
  1969. X
  1970. Xtypedef float Transform[4][3];
  1971. X
  1972. Xvoid BuildViewTransform( VRP, EP, UP, T )
  1973. X     Point3 VRP, EP, UP;
  1974. X     Transform T;
  1975. X{
  1976. X    Vector3    U, V, N;
  1977. X    float    dot;
  1978. X
  1979. X    /*
  1980. X     * Compute vector  N = EP - VRP  and normalize  N
  1981. X     */
  1982. X    N.x = EP.x - VRP.x; N.y = EP.y - VRP.y; N.z = EP.z - VRP.z;
  1983. X    V3Normalize(&N);
  1984. X
  1985. X    /*
  1986. X     * Compute vector  V = UP - VRP
  1987. X     * Make vector  V  orthogonal to  N  and normalize  V
  1988. X     */
  1989. X    V.x = UP.x - VRP.x; V.y = UP.y - VRP.y; V.z = UP.z - VRP.z;
  1990. X    dot = V3Dot(&V,&N);
  1991. X    V.x -= dot * N.x; V.y -= dot * N.y; V.z -= dot * N.z;
  1992. X    V3Normalize(&V);
  1993. X
  1994. X
  1995. X    /*
  1996. X     * Compute vector  U = V x N  (cross product)
  1997. X     */
  1998. X    V3Cross(&V,&N,&U);
  1999. X
  2000. X    /*
  2001. X     * Write the vectors U, V, and N as the first three rows of the
  2002. X     *       first, second, and third columns of  T, respectively
  2003. X     */
  2004. X    T[0][0] = U.x;        /* column 1 , vector U */
  2005. X    T[1][0] = U.y;
  2006. X    T[2][0] = U.z;
  2007. X    T[0][1] = V.x;        /* column 2 , vector V */
  2008. X    T[1][1] = V.y;
  2009. X    T[2][1] = V.z;
  2010. X    T[0][2] = N.x;        /* column 3 , vector N */
  2011. X    T[1][2] = N.y;
  2012. X    T[2][2] = N.z;
  2013. X
  2014. X    /*
  2015. X     * Compute the fourth row of  T  to include the translation of
  2016. X     *       VRP  to the origin
  2017. X     */
  2018. X    T[3][0] = - U.x * VRP.x - U.y * VRP.y - U.z * VRP.z;
  2019. X    T[3][1] = - V.x * VRP.x - V.y * VRP.y - V.z * VRP.z;
  2020. X    T[3][2] = - N.x * VRP.x - N.y * VRP.y - N.z * VRP.z;
  2021. X
  2022. X    return;
  2023. X}
  2024. X
  2025. X
  2026. END_OF_FILE
  2027. if test 1575 -ne `wc -c <'ViewTrans.c'`; then
  2028.     echo shar: \"'ViewTrans.c'\" unpacked with wrong size!
  2029. fi
  2030. # end of 'ViewTrans.c'
  2031. fi
  2032. echo shar: End of archive 1 \(of 5\).
  2033. cp /dev/null ark1isdone
  2034. MISSING=""
  2035. for I in 1 2 3 4 5 ; do
  2036.     if test ! -f ark${I}isdone ; then
  2037.     MISSING="${MISSING} ${I}"
  2038.     fi
  2039. done
  2040. if test "${MISSING}" = "" ; then
  2041.     echo You have unpacked all 5 archives.
  2042.     rm -f ark[1-9]isdone
  2043. else
  2044.     echo You still need to unpack the following archives:
  2045.     echo "        " ${MISSING}
  2046. fi
  2047. ##  End of shell archive.
  2048. exit 0
  2049.  
  2050.